﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DreamBuilder.Core.Message;
using DreamBuilder.Common;


namespace Web
{
    public partial class ErrorPage :Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitControlsCultureInfo();
            if (!this.IsPostBack)
            {
                MessageInfo loHandle = null;
                if (this.Session[MessageInfo.MsgKey] != null)
                {
                    if (this.Session[MessageInfo.MsgKey] is MessageInfo)
                    {
                        loHandle = this.Session[MessageInfo.MsgKey] as MessageInfo;
                    }
                    else
                    {
                        loHandle = MessageHandler.CreateMessage(this.Session[MessageInfo.MsgKey] as Exception);
                    }
                    this.Session[MessageInfo.MsgKey] = null;
                }
                else if (this.Application[MessageInfo.AppMsgKey] != null)
                {
                    if (this.Application[MessageInfo.AppMsgKey] is MessageInfo)
                    {
                        loHandle = this.Application[MessageInfo.AppMsgKey] as MessageInfo;
                    }
                    else
                    {
                        loHandle = MessageHandler.CreateMessage(this.Application[MessageInfo.AppMsgKey] as Exception);
                    }
                    this.Application[MessageInfo.AppMsgKey] = null;
                }
                this.ShowMessage(loHandle);
            }
        }

        /// <summary>
        /// 初始化控件多语信息
        /// </summary>
        private void InitControlsCultureInfo()
        {
            string PageName = Request.CurrentExecutionFilePath.Remove(0, Request.ApplicationPath.Length + 1).Replace(".aspx", "");
            //this.litErrorTitle.Text = CResManager.GetString(PageName,"litErrorTitle");
            //this.lblErrorCode.Text = CResManager.GetString(PageName, "lblErrorCode");
            //this.lblMessage.Text = CResManager.GetString(PageName, "lblMessage");
            //this.btnGoBack.Text = CResManager.GetString(PageName, "btnGoBack");
            this.litErrMsg.Text = "错误信息:";
        }
        
        private void ShowMessage(MessageInfo toHandle)
        {
            if (toHandle != null)
            {
                //this.litErrMsg.Text = toHandle.Code;
                this.litErrMsg.Text = toHandle.DetailMessage;
                this.btnLogin.OnClientClick = "window.location.href='" + HttpContext.Current.Request.ApplicationPath + "/Views/SystemLogin/SystemLogin.aspx';return false;";
                this.btnBack.OnClientClick = "window.location.href='" + HttpContext.Current.Request.ApplicationPath + "/Views/SystemLogin/SystemLogin.aspx';return false;";
                this.btnPortal.OnClientClick = "window.location.href='" + HttpContext.Current.Request.ApplicationPath + "/portal.aspx';return false;";
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //string scrtips = "GoToLogin('Views/SystemLogin/Login.aspx');";
            //Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", scrtips, true);
            this.Response.Redirect("Views/SystemLogin/SystemLogin.aspx");
        }

        protected void btnPortal_Click(object sender, EventArgs e)
        {
            //string scrtips = "GoToLogin('Views/SystemLogin/portal.aspx');";
            this.Response.Redirect("portal.aspx");
        }
    }
}
